<?php
class AdminDudoanKetQua extends CActiveRecord{
    
    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }
    
    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'dudoan';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){}
    
    public function insertRow($name,$boso,$id){
        $date = date('Y-m-d',time());
        $last_id =0;
        $connect = Yii::app()->db;
        $sql =   "INSERT INTO dudoan (province_name, province_id, content, date) VALUES (:province_name, :province_id, :content, :date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_name", $name, PDO::PARAM_STR );
        $command->bindValue ( ":province_id", $id, PDO::PARAM_STR ); 
        $command->bindValue ( ":content", $boso, PDO::PARAM_STR );
        $command->bindValue ( ":date", $date, PDO::PARAM_STR );           
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    
    public function insertRowDate($name,$boso,$id,$date){    
        $last_id =0;
        $connect = Yii::app()->db;
        $sql =   "INSERT INTO dudoan (province_name, province_id, content, date) VALUES (:province_name, :province_id, :content, :date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_name", $name, PDO::PARAM_STR );
        $command->bindValue ( ":province_id", $id, PDO::PARAM_STR ); 
        $command->bindValue ( ":content", $boso, PDO::PARAM_STR );
        $command->bindValue ( ":date", $date, PDO::PARAM_STR );           
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    
    public function updateRow($boso,$province_name){
        $date = date('Y-m-d',time()); 
        $sql = "UPDATE dudoan SET content='".$boso."' WHERE `date`='".$date."' AND province_name='".$province_name."'";       
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $result = $command->execute();
        return $result;
    }
    
    public function getRowByDate($date){
        $row = array();
        $sql = "SELECT province_name,content FROM dudoan WHERE `date`='".$date."'";
        
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $row = $command->queryAll();
        return $row;    
    }
    
    public function countRowInDate($from, $to){
        $row = array();
        $sql = "SELECT `date` FROM dudoan WHERE `date` >= '".$from."' AND `date` <= '".$to."' GROUP BY `date` ORDER BY `date` DESC";
        
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $row = $command->queryAll();
        return $row; 
    }
    
    public function getRowInDatePaging($from, $to, $begin, $end){
        $row = array();
        $sql = "SELECT `date` FROM dudoan WHERE `date` >= '".$from."' AND `date` <= '".$to."'  GROUP BY `date` ORDER BY `date` DESC LIMIT $begin,$end";
        
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $row = $command->queryAll();
        return $row; 
    }
    
    public function deleteRowByDate($date){
        $sql = "DELETE FROM dudoan WHERE date ='".$date."'";       
        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $result = $command->execute();
        return $result;        
    }
    

}
   